package com.example.chapter7.dao;


import com.example.chapter7.bean.NewsBean;
import com.example.chapter7.util.DBconnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 封装数据库的增删改查操作
 */
public class NewsDao {
    DBconnection db = new DBconnection();
    /**
     * 添加新闻
     * @param news
     * @return
     */
    public int add(NewsBean news){
        DBconnection db = new DBconnection();
        Connection conn = db.getConn();//conn代表MySQL链接
        String sql ="insert into news values(null,?,?,?,?)";//使用jdbc预处理对象添加
        try {
            PreparedStatement ps =conn.prepareStatement(sql);
            ps.setString(1,news.getNewsTitle());
            ps.setString(2,news.getNewsContent());
            ps.setString(3,news.getNewsTime());
            ps.setString(4,news.getAdminName());
            return ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("插入news表异常");

        }
        return 0;
    }

    /**
     * 删除功能
     * @param NewsID
     * @return
     */
    public int delete(int NewsID){
        Connection conn = db.getConn();
        String sql ="delete from news where NewsID =?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,NewsID);
            return ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("删除news表异常");
        }
        return 0;
    }


    public int update(NewsBean news){
        Connection conn = db.getConn();//conn代表MySQL链接
        String sql ="update news set NewsTitle=?,NewContent=?,NewsTime=?,AdminName=?where  NewsID=?";//使用jdbc预处理对象添加
        try {
            PreparedStatement ps =conn.prepareStatement(sql);
            ps.setString(1,news.getNewsTitle());
            ps.setString(2,news.getNewsContent());
            ps.setString(3,news.getNewsTime());
            ps.setString(4,news.getAdminName());
            ps.setInt(5,news.getNewsID());
            return ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("更新news表异常");
        }
        return 0;
    }

    /**
     * 获取新闻数据
     * @return
     */
    public List<NewsBean> getList(int page) {
        Connection conn = db.getConn();
        String sql = "select * from news limit ?,?";//第一个开始的行数 第二个是查询显示的行数
        List<NewsBean> list = new ArrayList<>();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            //固定一页显示10条数据
            ps.setInt(1, (page - 1) * 10);//page为1时行数是0 page为2时 10行
            ps.setInt(2, 10);
            ResultSet rs = ps.executeQuery();//rs是数据查询结果集 全部是字符串
            while (rs.next()) {
                NewsBean news = new NewsBean();
                news.setNewsID(rs.getInt("NewsID"));
                news.setNewsTitle(rs.getString("NewsTitle"));
                news.setNewsContent(rs.getNString("NesContent"));
                news.setNewsTime(rs.getString("NewsTime"));
                news.setAdminName(rs.getString("AdminName"));
                list.add(news);
            }
        } catch (SQLException e) {
            System.out.println("查询news表异常");
        }
        return list;
    }
}